#include "ChatMessageUI.h"
#include "Message/TextChatMessage.h"
#include "Message/TextChatMessagePainter.h"
#include "Message/ChatMessgeFramePainter.h"

#include <QPainter>
#include <QBitmap>

ChatMessageUI::ChatMessageUI(CChatMessage *pChatMessage,
                             QWidget *parent )
    :QWidget(parent),m_pChatMessage(pChatMessage){
   // setStyleSheet("background-color: rgb(255, 170, 0);");

    QPixmap pixmap;
    pixmap.load(":/image/qrc/image/headImage/16.bmp");

    m_pHeadImage = Pixmap2Round(pixmap, 15);
}

 CChatMessage* ChatMessageUI::GetMessage() const{
     return m_pChatMessage;
 }

void ChatMessageUI::paintEvent(QPaintEvent *e){
    Q_UNUSED(e);

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(Qt::gray));

    paintHeadImge(&painter);

    CTextChatMessagePainter txtMessagePainter(dynamic_cast<CTextChatMessage*>(m_pChatMessage));
    txtMessagePainter.PainterContexnt(painter);
   // CChatMessageFramePainter contextPainter;
  //  contextPainter.Decorator(&txtMessagePainter);
  //  contextPainter.PainterContexnt(painter);
//    ChatMessgeUIPainter& contextPainter = CChatMessageFramePainter();
//    contextPainter.PainterContexnt();

}

void ChatMessageUI::paintHeadImge(QPainter *painter){
    const QRect rectFrame(20, 10, 40, 40);
    painter->drawPixmap(rectFrame, m_pHeadImage);
}

QPixmap ChatMessageUI::Pixmap2Round(QPixmap& src, int radius){
    if (src.isNull()){
        return QPixmap();
    }

    QSize size(radius << 1, radius << 1);
    QBitmap mask(size);

    QPainter painter(&mask);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.fillRect(0, 0, size.width(), size.height(), Qt::white);

    painter.setBrush(QColor(0, 0, 0));
    painter.drawRoundedRect(0, 0, size.width(), size.height(), 99,99);

    QPixmap image = src.scaled(size);
    image.setMask(mask);
    return image;
}
